package _算法思维系列._双指针;

/**
 * @Author: 吕庆龙
 * @Date: 2020/4/15 11:56
 * <p>
 * 功能描述:
 */
public class Summary_0167 {

    public static void main(String[] args) {
        Summary_0167 test = new Summary_0167();
        int[] nums = new int[]{2, 4, 4, 7, 11, 15};
        int[] ints = test.twoSum(nums, 8);
        System.out.println(ints[0] + "---" + ints[1]);

    }

    /**
     * 给定已升序的数组 nums = [2, 7, 11, 15], target = 9
     * <p>
     * 因为 nums[0] + nums[1] = 2 + 7 = 9
     * 所以返回 [0, 1]
     */
    public int[] twoSum(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int sum = 0;
        while (left < right) {
            sum = nums[left] + nums[right];
            if (sum == target) {
                return new int[]{left + 1, right + 1};
            } else if (sum > target) {
                right--;
            } else if (sum < target) {
                left++;
            }
        }
        return new int[]{-1, -1};
    }
}
